package org.springframework.samples.mvc.employee;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@Scope("prototype")
@RequestMapping("/employee.do")
@SessionAttributes("employeeModel")
public class EmployeeContoller {
	private String view_page = "employee/EmployeeForm2";
	private String success_page = "employee/EmployeeSuccessForm";
	
	private EmployeeValidator validator;
	
	@Autowired
	public EmployeeContoller(EmployeeValidator employeeValidator){
		this.validator = employeeValidator;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String initForm(Model model){
		EmployeeModel employeeModel = new EmployeeModel();
		model.addAttribute("employeeModel", employeeModel);
		
		return view_page;
	}
	
	Long PLACE_KEY;
	private boolean isDoubleSubmit(HttpSession session){
		System.out.println(PLACE_KEY);
		System.out.println(session.getAttribute("LAST_PLACE_KEY"));
		
		Random generator = new Random(19580427);
		if(PLACE_KEY==null){
			PLACE_KEY = generator.nextLong();
		}
		
		if (((Long) session.getAttribute("LAST_PLACE_KEY"))!=null && ((Long) session.getAttribute("LAST_PLACE_KEY")).equals(PLACE_KEY)) {
			return true;
		}else{
			session.setAttribute("LAST_PLACE_KEY", PLACE_KEY);			
			return false;
		}
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(){	
		return view_page;
	}
	
	@RequestMapping(method = RequestMethod.POST, params="execute=save")
	public String save(@ModelAttribute("employeeModel") EmployeeModel employeeModel, 
			BindingResult result, SessionStatus status){
		System.out.println("save....");
		
		this.validator.validate(employeeModel, result);
		
		if(result.hasErrors()){
			//if validator failed
			return view_page;
		}else{
			//Complete
			status.setComplete();
			//form success
			return success_page;
		}
	}
	
	@RequestMapping(method = RequestMethod.POST, params="execute=add")
	public String add(@ModelAttribute("employeeModel") EmployeeModel employeeModel, 
			BindingResult result, HttpSession session){
		System.out.println("add....");

		for(int i=0; i<1000; i++){
		Employee employee = new Employee();
		employee.setEmpNo( employeeModel.getEmployees().size()+"" );
		employee.setCreateDate( new Date() );
		employeeModel.addEmployee(employee);
		}
		
		return view_page;
	}
	
	@RequestMapping(method = RequestMethod.POST, params="execute=delete")
	public String delete(@ModelAttribute("employeeModel") EmployeeModel employeeModel, 
			BindingResult result, @RequestParam("deleteId") int id){
		System.out.println("delete....");
		
		employeeModel.deleteEmployee(id);
		
		return view_page;
	}
	
	@RequestMapping(method = RequestMethod.POST, params="execute=test")
	public String test(@ModelAttribute("employeeModel") EmployeeModel employeeModel, 
			BindingResult result){
		System.out.println("test....");
		
		result.reject(null, "Test Error 1 ...");
		result.reject(null, "Test Error 2 ...");
		result.reject(null, "Test Error 3 ...");
		
		return view_page;
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");		
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	@ModelAttribute("sexSelectMap")
	public Map<String,String> sexSelectMap() {
		Map<String,String> sexSelectMap = new LinkedHashMap<String,String>();
		sexSelectMap.put("M", "Man");
		sexSelectMap.put("F", "Female");
		
		return sexSelectMap;
	}
	
	@ModelAttribute("deptSelectList")
	public List<String> deptSelectList() {
		List<String> deptList = new ArrayList<String>();
		deptList.add("A100");
		deptList.add("A110");
		deptList.add("A120");
		deptList.add("A130");
		deptList.add("A140");
		
		return deptList;
	}
}
